import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jet_rails/jr_app/jr_views/jr_login/jr_login_view.dart';
import 'package:jet_rails/jr_app/jr_views/jr_splash/jr_splash_view.dart';
import 'package:jet_rails/jr_constant/jr_keys.dart';
import 'package:jet_rails/jr_services/jr_services.dart';
import 'package:jet_rails/jr_utils/jr_logger.dart';
import 'package:jet_rails/jr_utils/jr_shared_preferences.dart';

class JrApp extends StatefulWidget {
  const JrApp({super.key});

  @override
  State<JrApp> createState() => JrAppState();
}

class JrAppState extends State<JrApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      builder: (context, child) {
        return app();
      },
    );
  }

  Widget app() {
    return GetMaterialApp(
      title: "JetRails",
      debugShowCheckedModeBanner: false,
      routingCallback: (Routing? value) {
        JrLogger.d('routingCallback: prev=>${value?.previous} current=>${value?.current} ');
      },
      // navigatorObservers: [JrAppUtil.routeObserver],
      builder: EasyLoading.init(),
      initialBinding: BindingsBuilder.put(() => JrServices()),
      home: const JrAppBody(),
    );
  }
}

class JrAppBody extends StatefulWidget {
  const JrAppBody({super.key});

  @override
  State<JrAppBody> createState() => JrAppBodyState();
}

class JrAppBodyState extends State<JrAppBody> {
  bool showSplash = true;

  @override
  void initState() {
    super.initState();
    checkSplash();
  }

  void checkSplash() {
    showSplash = JrSP.instance.getBool(JrKeys.kShowSplash) ?? true;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (showSplash) {
      return const JrSplashView();
    } else {
      return const JrLoginView();
    }
  }
}
